﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Autorun
{
    public partial class PaneTasks : Autorun.PaneBase
    {

        #region "Constructors and overriden methods"
        public PaneTasks()
        {
            InitializeComponent();

            LoadIcons();
        }

        public override bool Initialize(Autorun.IPresenter sender, Autorun.ShowPaneEventArgs e)
        {

            _parent = sender;
            _parent.OnChangingPane += new ChangePaneEventHandler(_parent_OnChangingPane);

            if ((e != null) && (e.Args != null) && (e.Args.Length != 0))
                if (e.Args[0] is ServerManagement)
                    _proxy = (ServerManagement)e.Args[0];
                else
                    _proxy = null;
            else
                _proxy = null;

            RefreshInformation();

            return true;
        }
        #endregion

        #region "Procedures"
        private void LoadIcons()
        {
            myImages.Images.Add(Properties.Resources.icoTaskItem);
        }

        private void RefreshInformation()
        {
            ListViewItem lvi;

            myList.Items.Clear();

            if (_proxy != null)
            {
                Caption = string.Format("{0}: Registered tasks", _proxy.Information.MachineName);

                foreach (TaskConfigurationItem item in _proxy.Configuration.Tasks.Items)
                {
                    lvi = myList.Items.Add(item.Caption, 0);
                    lvi.Tag = item.Id;
                    lvi.SubItems.Add(item.AssemblyName);
                    lvi.SubItems.Add(item.ClassName);
                    lvi.SubItems.Add(item.IsUsed.ToString());
                    lvi.SubItems.Add(item.Path);
                }
                myList.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                myList.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                myList.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                myList.Columns[3].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                myList.Columns[4].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
            else
                Caption = "Not connected";

        }
        #endregion


        #region "Event Handlers"
        private void mnuNew_Click(object sender, EventArgs e)
        {
            BrowseFileDialog dlg;
            TaskConfigurationItem task;
            TaskDialog dlgTsk;
            ScheduleDialog dlgSch;

            Cursor = Cursors.WaitCursor;
            dlg = new BrowseFileDialog();
            dlg.SetData(_proxy);
            Cursor = Cursors.Default;


            if (dlg.ShowDialog() == DialogResult.OK)
            {
                task = _proxy.Configuration.Tasks.Add(
                                        dlg.ClassName,
                                        dlg.AssemblyName,
                                        dlg.AssemblyPath,
                                        dlg.ClassName);

                dlgTsk = new TaskDialog();
                dlgTsk.SetData(_proxy, task.Id);
                if (dlgTsk.ShowDialog() == DialogResult.OK)
                {
                    dlgSch = new ScheduleDialog();
                    dlgSch.SetData(_proxy, task);
                    dlgSch.ShowDialog();
                }
                RefreshInformation();
            }
        }

        private void mnuDelete_Click(object sender, EventArgs e)
        {
            DialogResult rc;
            if (myList.SelectedItems.Count != 0)
            {

                rc = MessageBox.Show(string.Format("Do you want to remove the task '{0}' even if it is scheduled?", myList.SelectedItems[0].SubItems[0].Text), Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                switch (rc)
                {
                    case DialogResult.Yes:
                        _proxy.Configuration.Tasks.Remove((int)myList.SelectedItems[0].Tag, true);
                        RefreshInformation();
                        if (myList.Items.Count != 0) myList.Items[0].Selected = true;
                        break;
                    case DialogResult.No:
                        _proxy.Configuration.Tasks.Remove((int)myList.SelectedItems[0].Tag, false);
                        RefreshInformation();
                        break;
                    case DialogResult.Cancel:
                        break;
                    default:
                        throw new Exception("Unknown Dialog Result");
                }
            }
        }


        private void mnuEdit_Click(object sender, EventArgs e)
        {
            TaskDialog dlg;

            if (myList.SelectedItems.Count != 0)
            {
                dlg = new TaskDialog();
                dlg.SetData(_proxy, (int)myList.SelectedItems[0].Tag);
                if (dlg.ShowDialog() == DialogResult.OK)
                    RefreshInformation();
            }
        }

        private void mnuVerify_Click(object sender, EventArgs e)
        {
            if (myList.SelectedItems.Count != 0)
            {
                _proxy.Configuration.Tasks.Verify((int)myList.SelectedItems[0].Tag);
                mnuEdit_Click(sender, e);
            }
        }

        private void myPopup_Opening(object sender, CancelEventArgs e)
        {
            bool selected;

            selected = (myList.SelectedItems.Count != 0);
            mnuDelete.Enabled = selected;
            mnuVerify.Enabled = selected;
        }


        private void mnuRun_Click(object sender, EventArgs e)
        {
            if (myList.SelectedItems.Count != 0)
            {
                _proxy.Scheduler.PlanTask((int)myList.SelectedItems[0].Tag);
            }
        }
        #endregion

    }
}
